package com.easyskool.filter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import org.codehaus.jackson.map.ObjectMapper;

import com.easyskool.utilfunction.NullObjects;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

public class ServiceFilter implements ContainerRequestFilter{

	
	@Context
	HttpServletRequest servletRequest;
	@Override
	public ContainerRequest filter(ContainerRequest request) {
		
		System.out.println("request : "+request);
		URI reqUri = request.getRequestUri();
		System.out.println("req uri "+reqUri );
		BufferedReader inFromClient = null;
		
		try {
			
			inFromClient = new BufferedReader(new InputStreamReader(
					request.getEntityInputStream(), "UTF-8"));
			
		} catch (UnsupportedEncodingException e1) {
			
			e1.printStackTrace();
			
		}

		String line;
		
		StringBuffer buff = new StringBuffer();
		
		try {
			
			while ((line = inFromClient.readLine()) != null) {
				
				buff.append(line);
				
			}
			
			inFromClient.close();
			
		} catch (IOException e) {
			
			e.printStackTrace();
			
		}

		System.out.println("###### Request parameters- " + buff);
 
		
		String serviceMethodName = request.getPathSegments()
				.get(request.getPathSegments().size() - 1).getPath();

 
		System.out.println("request"+request);
		System.out.println("request URI: "+request.getRequestUri());
		System.out.println("request-QueryParameters-"+request.getQueryParameters());
		return request;
	}
}
